<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="100px">
      <el-form-item style="margin-left: -30px" label="乘客姓名" prop="passengerName">
        <el-input
          v-model="queryParams.passengerName"
          placeholder="请输乘客姓名"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 180px"
        />
      </el-form-item>
      <el-form-item style="margin-left: -10px" label="乘客证件号" prop="passengerIdCard">
        <el-select
          v-model="queryParams.passengerIdCard"
          filterable
          remote
          :remote-method="handleSearch"
          placeholder="请输入身份证后6位"
        >
          <el-option
            v-for="item in options"
            :key="item.idCard"
            :label="formatIdCard(item.idCard)"
            :value="item.idCard"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="zaodaoCkxlList" >
      <el-table-column label="姓名" align="center" prop="passengerName" width="120"/>
      <el-table-column label="证件号" align="center" prop="passengerIdCard" width="250"/>
      <el-table-column label="库位编号" align="center" prop="tpcsXlzdKwBh" width="120">
        <template slot-scope="scope">
          <span>{{scope.row.tpcsXlzdKwBh}}号</span>
        </template>
      </el-table-column>
      <el-table-column class-name="small-padding fixed-width" label="航班号" align="center" prop="flightNo">
      </el-table-column>
      <el-table-column label="预计出发" align="center" prop="takeOffTime" width="160">
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listZaodaoCkxl } from "@/api/ckxl/zaodao";
import { getCkIdCardInfo } from '@/api/ckxl/ck'
import { debounce } from 'lodash';

export default {
  name: "ZaoDao",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 航班表格数据
      zaodaoCkxlList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        passengerName: '',
        passengerIdCard: '',
      },
      options: [{
        label:"",
        value:""
      }],
    };
  },
  created() {
    this.getList();
  },
  computed:{
  },
  methods: {

    /** 查询航班列表 */
    getList() {
      this.loading = true;
      listZaodaoCkxl(this.queryParams).then(response => {
        this.zaodaoCkxlList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 表单重置
    reset() {
      this.form= {};
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryIsTransfer = '';
      this.handleQuery();
    },

    // 防抖处理（500ms内仅触发一次）
    handleSearch: debounce(function(query) {
      if(query.trim().length >6){
        const keyword = query.trim().slice(0, 6).toUpperCase();
        this.$modal.msgError("最多只能输入6个字符");
        this.queryParams.passengerIdCard = keyword;
        return;
      }
      // 校验格式：6位数字或X
      if (!/^[0-9X]{6}$/.test(query.trim())) {
        this.options = [];
        return;
      }
      const lastSixIdNum = query.trim();
      getCkIdCardInfo(lastSixIdNum).then(response => {
        if (response.data != null) {
          this.options = response.data
        }
      }).catch(() => {
        this.options = [];
      });
    }, 500),

    // 格式化显示身份证号（如：410*****123456）
    formatIdCard(idCard) {
      if(idCard != null){
        return idCard.replace(/(\d{4})\d+(\d{6})/, '$1*****$2');
      }
    },

  }
};
</script>

<style lang="scss" scoped>

</style>
